var express = require('express');
const { Mongoose } = require('mongoose');
var router = express.Router();// 路由
const SchedulesApi = require("../api/schedules");


// 获取排片数据
router.get("/get", async function (req, res, next) {
    const { curPage, eachPage } = req.query;
    const data = await SchedulesApi.get({ curPage, eachPage });
    res.send(data)
});
// 获取电影
router.get("/getMovie", async function (req, res, next) {
    const data = await SchedulesApi.getMovie();
    res.send(data)
});

// 获取影院
router.get("/getCinemas", async function (req, res, next) {
    const data = await SchedulesApi.getCinemas();
    res.send(data)
});

// 获取放映厅
router.get("/getTheaters/:_id", async function (req, res, next) {
    const { _id } = req.params;
    const data = await SchedulesApi.getTheaters({ _id });
    console.log(data);
    res.send(data)
});

// 新增排片
router.post("/addSchedules", async function (req, res, next) {
    const { movieId, cinemasId, theaterId, showTime, price } = req.body;
    const data = await SchedulesApi.addSchedules({ movieId, cinemasId, theaterId, showTime, price });
    res.send(data)
});
// 删除排片
router.delete("/removeSchedules/:_id", async function (req, res, next) {
    const { _id } = req.params;
    const data = await SchedulesApi.removeSchedules({ _id });
    res.send(data)
});

//  获取排片的座位
router.get("/getSeatings/:_id", async function (req, res, next) {
    const { _id } = req.params;
    const data = await SchedulesApi.getSeatings(_id);
    console.log(data);
    res.send(data)
});

//  通过排片id找到整条数据
router.get("/getsortmoviesData/:_id", async function (req, res, next) {
    const { _id } = req.params;
    const data = await SchedulesApi.sortmoviesData(_id);
    res.send(data)
});

//  新增订单
router.post("/addOrder", async function (req, res, next) {
    const { shopSeatId, userId } = req.body;
    // 因传入时转换JSON格式，所以这里需要转换
    const data = JSON.parse(shopSeatId);
    const status = await SchedulesApi.addOrder(data, userId);
    res.send(status);
});

//  查询电影
router.get("/findMovie/:_id", async function (req, res, next) {
    const { _id } = req.params;
    const data = await SchedulesApi.findMovie({ _id });
    console.log(data);
    res.send(data);
});

module.exports = router;
